package com.example.idachuappone.order.item;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.person.entity.Order;
import com.example.idachuappone.person.entity.OrderState;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class MyOrderDetailTimeTwoActivity extends BaseActivity implements OnClickListener {
	private Button btn_back;
	private ListView lv_main;
	private MyOrderStateAdapter myOrderStateAdapter;
	private Order order;
	private HashMap<String, String> orderStatehHashMap;
	private List<OrderState> orderStateliList;

	private class MyOrderStateAdapter extends BaseAdapter {
		private Context context;
		private List<OrderState> list;
		private Order order;

		public MyOrderStateAdapter(Context context) {
			this.context = context;
		}

		public void setList(List<OrderState> list) {
			this.list = list;
		}

		public void setOrder(Order order) {
			this.order = order;
		}

		@Override
		public int getCount() {
			return this.list == null ? 0 : this.list.size();
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public Object getItem(int position) {
			return this.list == null ? null : (OrderState) this.list.get(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = LinearLayout.inflate(this.context, R.layout.simple_my_order_state_item, null);
				viewHolder.tv_order_state = (TextView) convertView.findViewById(R.id.tv_order_state);
				viewHolder.tv_order_state_detail = (TextView) convertView.findViewById(R.id.tv_order_state_detail);
				viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
				viewHolder.tv_xian_shang = (TextView) convertView.findViewById(R.id.tv_xian_shang);
				viewHolder.tv_xian_shang1 = (TextView) convertView.findViewById(R.id.tv_xian_shang1);
				viewHolder.tv_xian_xia = (TextView) convertView.findViewById(R.id.tv_xian_xia);
				viewHolder.img_state_now = (ImageView) convertView.findViewById(R.id.img_state_now);
				viewHolder.img_state_ok = (ImageView) convertView.findViewById(R.id.img_state_ok);
				viewHolder.ll_state_now = (LinearLayout) convertView.findViewById(R.id.ll_state_now);
				viewHolder.ll_state_ok = (LinearLayout) convertView.findViewById(R.id.ll_state_ok);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			OrderState orderState = this.list.get(position);
			viewHolder.tv_order_state.setText(MyOrderDetailTimeTwoActivity.this.orderStatehHashMap.get(
					orderState.getOperation()).substring(0,
					MyOrderDetailTimeTwoActivity.this.orderStatehHashMap.get(orderState.getOperation()).indexOf(" ")));
			viewHolder.tv_order_state_detail.setText(MyOrderDetailTimeTwoActivity.this.orderStatehHashMap.get(
					orderState.getOperation())
					.substring(
							MyOrderDetailTimeTwoActivity.this.orderStatehHashMap.get(orderState.getOperation())
									.indexOf(" ") + 1));
			if (orderState.getOperation().equals("create") && Integer.parseInt(this.order.getPay_type()) == 1) {
				viewHolder.tv_order_state.setText("\u8ba2\u5355\u63d0\u4ea4\u6210\u529f");
				viewHolder.tv_order_state_detail.setText("\u8bf7\u8010\u5fc3\u7b49\u5f85\u5ba2\u670d\u5904\u7406");
			}
			if (orderState.getOperation().equals("cancel")) {
				if (Integer.parseInt(this.order.getPay_type()) == 0) {
					viewHolder.tv_order_state.setText("\u8ba2\u5355\u5df2\u53d6\u6d88");
					viewHolder.tv_order_state_detail.setText("\u5df2\u5173\u95ed\u8ba2\u5355");
				}
				if (Integer.parseInt(this.order.getPay_type()) == 2
						&& Double.valueOf(this.order.getPay_status()).doubleValue() == 0.0d) {
					viewHolder.tv_order_state.setText("\u8ba2\u5355\u5df2\u53d6\u6d88");
					viewHolder.tv_order_state_detail.setText("\u5df2\u5173\u95ed\u8ba2\u5355");
				}
				if (Integer.parseInt(this.order.getPay_type()) == 1) {
					viewHolder.tv_order_state.setText("\u8ba2\u5355\u5df2\u53d6\u6d88");
					viewHolder.tv_order_state_detail.setText("\u5df2\u5173\u95ed\u8ba2\u5355");
				}
			}
			viewHolder.ll_state_ok.setBackgroundResource(R.drawable.img_order_state_round_no);
			String time = orderState.getDateline().substring(orderState.getDateline().indexOf("-") + 1);
			viewHolder.tv_time.setText(time.substring(0, time.lastIndexOf(":")));
			if (position != 0) {
				viewHolder.tv_order_state.setTextColor(MyOrderDetailTimeTwoActivity.this.getResources().getColor(
						R.color.black));
				viewHolder.tv_order_state_detail.setTextColor(Color.parseColor("#bfbfbf"));
				viewHolder.tv_time.setTextColor(MyOrderDetailTimeTwoActivity.this.getResources().getColor(
						R.color.color_two_lable_txt));
				viewHolder.img_state_now.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				viewHolder.img_state_ok.setVisibility(0);
				viewHolder.ll_state_now.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				viewHolder.ll_state_ok.setVisibility(0);
			} else if (Integer.parseInt(this.order.getStatus()) != 4) {
				viewHolder.tv_xian_shang.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR);
				viewHolder.tv_xian_shang1.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR);
				viewHolder.tv_order_state.setTextColor(MyOrderDetailTimeTwoActivity.this.getResources().getColor(
						R.color.color_two_main));
				viewHolder.tv_order_state_detail.setTextColor(MyOrderDetailTimeTwoActivity.this.getResources()
						.getColor(R.color.color_two_main));
				viewHolder.tv_time.setTextColor(MyOrderDetailTimeTwoActivity.this.getResources().getColor(
						R.color.color_two_main));
				viewHolder.img_state_now.setVisibility(0);
				viewHolder.img_state_ok.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				viewHolder.ll_state_now.setVisibility(0);
				viewHolder.ll_state_ok.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			} else {
				viewHolder.tv_xian_shang.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR);
				viewHolder.tv_xian_shang1.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR);
				viewHolder.tv_order_state.setTextColor(MyOrderDetailTimeTwoActivity.this.getResources().getColor(
						R.color.black));
				viewHolder.tv_order_state_detail.setTextColor(Color.parseColor("#bfbfbf"));
				viewHolder.tv_time.setTextColor(MyOrderDetailTimeTwoActivity.this.getResources().getColor(
						R.color.color_two_lable_txt));
				viewHolder.img_state_now.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				viewHolder.img_state_ok.setVisibility(0);
				viewHolder.ll_state_now.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				viewHolder.ll_state_ok.setVisibility(0);
			}
			if (position == this.list.size() - 1) {
				viewHolder.tv_xian_xia.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR);
			} else {
				viewHolder.tv_xian_xia.setVisibility(0);
			}
			if (orderState.getOperation().equals("finish")) {
				viewHolder.ll_state_now.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				viewHolder.ll_state_ok.setVisibility(0);
				viewHolder.ll_state_ok.setBackgroundResource(R.drawable.img_order_state_round_p);
				viewHolder.img_state_ok.setVisibility(0);
			}
			return convertView;
		}
	}

	private class ViewHolder {
		private ImageView img_state_now;
		private ImageView img_state_ok;
		private LinearLayout ll_state_now;
		private LinearLayout ll_state_ok;
		private TextView tv_order_state;
		private TextView tv_order_state_detail;
		private TextView tv_time;
		private TextView tv_xian_shang;
		private TextView tv_xian_shang1;
		private TextView tv_xian_xia;

		private ViewHolder() {
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_order_detail_time_two);
		this.orderStateliList = (List) getIntent().getExtras().getSerializable("orderStateliList");
		this.order = (Order) getIntent().getExtras().getSerializable("order");
		initDataTime();
		initView();
	}

	private void initView() {
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.lv_main = (ListView) findViewById(R.id.lv_main);
		this.myOrderStateAdapter = new MyOrderStateAdapter(this);
		this.lv_main.setAdapter(this.myOrderStateAdapter);
		initAdapter();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		default:
		}
	}

	private void initAdapter() {
		this.myOrderStateAdapter.setOrder(this.order);
		this.myOrderStateAdapter.setList(this.orderStateliList);
		this.myOrderStateAdapter.notifyDataSetChanged();
	}

	private void initDataTime() {
		this.orderStatehHashMap = new HashMap();
		this.orderStatehHashMap.put("create",
				"\u8ba2\u5355\u63d0\u4ea4\u6210\u529f \u8bf7\u53ca\u65f6\u652f\u4ed8\u8ba2\u5355");
		this.orderStatehHashMap.put("cancel", "\u8ba2\u5355\u5df2\u53d6\u6d88 \u9000\u6b3e\u5904\u7406\u4e2d");
		this.orderStatehHashMap.put("finish", "\u5927\u53a8\u5df2\u5b8c\u6210\u670d\u52a1 \u8ba2\u5355\u5b8c\u6210");
		this.orderStatehHashMap
				.put("start",
						"\u5ba2\u670d\u5df2\u7ecf\u5904\u7406\u8ba2\u5355 \u8bf7\u8010\u5fc3\u7b49\u5f85\u5927\u53a8\u4e0a\u95e8\u670d\u52a1");
		this.orderStatehHashMap
				.put("weixin_pay",
						"\u8ba2\u5355\u5df2\u7ecf\u5b8c\u6210\u652f\u4ed8 \u8bf7\u8010\u5fc3\u7b49\u5f85\u5ba2\u670d\u5904\u7406");
		this.orderStatehHashMap
				.put("alipay_pay",
						"\u8ba2\u5355\u5df2\u7ecf\u5b8c\u6210\u652f\u4ed8 \u8bf7\u8010\u5fc3\u7b49\u5f85\u5ba2\u670d\u5904\u7406");
		this.orderStatehHashMap
				.put("balance_pay",
						"\u8ba2\u5355\u5df2\u7ecf\u5b8c\u6210\u652f\u4ed8 \u8bf7\u8010\u5fc3\u7b49\u5f85\u5ba2\u670d\u5904\u7406");
		this.orderStatehHashMap.put("working",
				"\u5927\u53a8\u4e0a\u95e8\u670d\u52a1\u4e2d \u8bf7\u8010\u5fc3\u7b49\u5f85\u5927\u53a8\u670d\u52a1");
	}
}
